﻿@inject IActionService _actionService
@inject ChatState _chatState
@implements IDisposable

<MudDrawerContainer Class="mud-height-full">
    <MudDrawer @bind-Open="IsOpen" Anchor="Anchor.Left" DisableOverlay="true" Elevation="8">
        @if (isEditing)
        {
            <DrawerHeader Title="@(SelectedActionId == Guid.Empty ? "Create a new action" : "Edit the action")" OnClose="CloseDrawer" />
            <ActionForm ActiontId="SelectedActionId" OnActionSubmit="HandleActionSubmitted" />
        }
        else
        {
            <DrawerHeader Title="Select or Manage Actions" OnClose="CloseDrawer" DisplayDivider="false" />
            <MudList Clickable="true" Class="pt-0" @bind-SelectedItem="SelectedItem" @bind-SelectedValue="SelectedValue" Color="@(_IsDarkModeEnabled ? Color.Tertiary : Color.Dark)">
                <MudDivider />
                @foreach (var group in Actions.GroupBy(a => a.Category))
                {
                    <MudListSubheader Class="pb-1">
                        @group.Key
                    </MudListSubheader>
                    @foreach (var action in group)
                    {
                        <ActionItem Action="@action"
                                    OnEdit="() => EditAction(action.Id)"
                                    OnDelete="HandleActionSubmitted" />
                    }
                }
            </MudList>
            <MudSpacer />
            <MudPaper class="d-flex flex-row justify-end gap-4 pa-4" Style="@BackgroundColorStyle">
                <MudButton OnClick="CreateNewAction" Variant="Variant.Filled" Color="Color.Primary">New Action</MudButton>
            </MudPaper>
        }
    </MudDrawer>
</MudDrawerContainer>

@code 
{
    [Parameter]
    public bool IsOpen { get; set; }

    [Parameter]
    public EventCallback<bool> IsOpenChanged { get; set; }

    [CascadingParameter]
    private bool _IsDarkModeEnabled { get; set; }

    private MudListItem? SelectedItem { get; set; }
    private object? SelectedValue { get; set; }
    private Guid SelectedActionId { get; set; }

    private string BackgroundColorStyle => $"background-color: {(_IsDarkModeEnabled ? "#1e1f22" : "#d6d3d1")};";
    private bool isEditing = false;

    public List<NativeActionResponse> Actions { get; set; } = new();

    protected override async Task OnInitializedAsync()
    {
        _chatState.ActionIdChanged += StateHasChanged;

        await LoadActions();
    }

    private async Task LoadActions()
    {
        var response = await _actionService.BrowseActions();
        var actions = response.Value;

        if (actions is not null)
        {
            Actions = actions.ToList();
        }
    }

    private void EditAction(Guid id)
    {
        _chatState.SetActionId(id);
        SelectedActionId = _chatState.ActionId;
        isEditing = true;
    }

    private async Task HandleActionSubmitted()
    {
        isEditing = false;
        await LoadActions();
    }

    private void CloseDrawer()
    {
        IsOpen = false;
        isEditing = false;
        IsOpenChanged.InvokeAsync(IsOpen);
    }

    private void CreateNewAction()
    {
        _chatState.SetActionId(Guid.Empty);
        SelectedActionId = _chatState.ActionId;
        isEditing = true;
    }

    public void Dispose()
    {
        _chatState.ActionIdChanged -= StateHasChanged;
    }
}
